import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, Query } from '@nestjs/common';
import { CommentsService } from './comments.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { User } from '../users/entities/user.entity';
import { Request } from 'express';
import { FindAllCommentDto } from './dto/findAll-comment.dto';
import { RoleGuard } from '../auth/role-auth.guard';
import { FindOneCommentDto } from './dto/findOne-comment.dto';
import { Throttle } from '@nestjs/throttler';

@Controller('comments')
export class CommentsController {
  constructor(private readonly commentsService: CommentsService) {}

  @Throttle({ default: { limit: 1, ttl: 10000 } })
  @UseGuards(JwtAuthGuard)
  @Post()
  create(@Body() createCommentDto: CreateCommentDto, @Req() request: Request) {
    return this.commentsService.create(createCommentDto, (request.user as User).id);
  }

  @Get()
  findAll(@Query() query: FindAllCommentDto) {
    return this.commentsService.findAll(query);
  }

  @Get('/child')
  findOne(@Query() query: FindOneCommentDto) {
    return this.commentsService.findOne(query);
  }

  @Get('/array')
  findAllArray(@Query() query: FindAllCommentDto) {
    return this.commentsService.findAllArray(query);
  }

  @UseGuards(JwtAuthGuard, RoleGuard)
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateCommentDto: UpdateCommentDto) {
    return this.commentsService.update(+id, updateCommentDto);
  }

  @UseGuards(JwtAuthGuard, RoleGuard)
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.commentsService.remove(+id);
  }
}
